<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worksheet_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}

	public function get_all_worksheet_by_date($where){	
	
	 $query = $this->db->get_where('worksheet',$where)->result();	
	/* echo '<pre>';
		print_r($query);*/
	 return ($query);
	$this->db->close();
	} 
	
	public function get_total_working_hr($where,$date_id){
		//echo $date_id;
		//
		//if($this->db->get_where('daily_attendance_sheet',$where)->row()->$date_id)
		//{	
		//$this->db->get_where('daily_attendance_sheet',$where)->row()->$date_id;
		if(!$this->db->select($date_id)->get_where('daily_attendance_sheet',$where)->row()){
			//echo 'sad';
			return FALSE;
		}
		else{
			
			//$this->db->select($date_id);
			//$this->db->from('daily_attendance_sheet');
			//$this->db->where($where);
			
			return $this->db->get_where('daily_attendance_sheet',$where)->row()->$date_id;
			//return $this->db->last_query();
		}
				
	}
	
	public function get_months(){
		return $this->db->distinct('month_id')->select('month_id,year_id')->get('worksheet')->result();
	}
	
	public function get_entry_time($where){
		//if($this->db->get_where('attendance_timings',$where)->row()->entry_time){
		
		$this->db->select('entry_time');
		$this->db->from('attendance_timings');
		$this->db->where($where);
		return $this->db->get()->row_array();
		//$this->db->get();
		//return $this->db->last_query();
		//
		//}
		//else{
		//	return FALSE;
			
		//}
		
	}
	public function get_exit_time($where){
		//return $this->db->get_where('attendance_timings',$where)->row()->exit_time;
		$this->db->select('*');
		$this->db->from('attendance_timings');
		$this->db->where($where);
		return $this->db->get()->row_array();
	}
	public function get_all_time($where){
		$this->db->select('*');
		$this->db->from('attendance_timings');
		$this->db->where($where);
		return $this->db->get()->row_array();
	}
	
	public function add_worksheet_remark($where)
	{			
	$this->db->insert('worksheet_remarks', $where);
	}	
	
	public function get_all_hashtag()
	{
		return $this->db->get('worksheet_hashtag')->result();	
	}		
	 
	
	public function get_remarks($where){
		$this->db->select('*');
		$this->db->from('worksheet_remarks');
		$this->db->where($where);
		return $this->db->get()->result();
	}
	public function get_all_remarks($where){
		$this->db->select('*');
		$this->db->from('worksheet_remarks');
		$this->db->where($where);
		return $this->db->get()->result();
	}
	public function delete_remarks($id)
	{
		$this->db->delete('worksheet_remarks',array('id'=>$id));
	}
	
	public function delete_hashtag($id)
	{
		$this->db->delete('worksheet_hashtag',array('id'=>$id));
	}
	
	public function insert_hashtag($tag)
	{
		$this->db->insert('worksheet_hashtag',array('tag'=>$tag));
	}
	
	public function add_work_color($id,$color){
	$this->db->update('worksheet',array('feed'=>$color),array('id'=>$id));
	}
	
	public function delete_worksheet($id)
	{
		$this->db->delete('worksheet',array('id'=>$id));
	}
	public function update($update,$id){
	$this->db->update('worksheet',$update,array('id'=>$id));
	}
	public function get_details($id){
		return $this->db->get_where('worksheet',array('id'=>$id))->row_array();
	}
/*	public function worksheet_verified($where)
	{	
		//echo '<pre>';
		//print_r($where);exit;
		foreach($where as $insert)
		{
			$this->db->insert('worksheet_verify', $insert);
		}
	}
	*/
	function worksheet_verified($where)
	{
		
		foreach($where as $get)
		{
	$get_id = $this->db->get_where('worksheet_verify',array('user_id'=>$get['user_id'],'date_id'=>$get['date_id'],'month_id'=>$get['month_id'],'year_id'=>$get['year_id']))->result();
		}
		
		if(count($get_id)==0)
					{	
		foreach($where as $insert)
		{
					$this->db->insert('worksheet_verify',$insert);
		}}else{	
		foreach($where as $update)
		{	
			$this->db->delete('worksheet_verify',array('user_id'=>$update['user_id'],'date_id'=>$update['date_id'],'year_id'=>$update['year_id'],'month_id'=>$update['month_id'],'verify'=>$update['verify']));
			
		}}
		
	}
	
	function get_worksheet_verify($where)
	{
		return $this->db->get_where('worksheet_verify',$where)->result();
	}
	
	
	/**
	* 
	* @param undefined $datas
	* 
*/
	function get_settings($setting_name){
	$this->db->select('*');
	$this->db->from('settings');	
	$this->db->like('name',$setting_name);
	$result = $this->db->get()->result_array();	
	return $result;
	}
	
	function get_single_setting($name){
		$this->db->from('settings');
		$this->db->like('name',$name);
		$result = $this->db->get()->row()->value;
		return $result;
	}
	
	function save_settings($datas){				
		foreach($datas as $key=>$val){		
			$this->db->update('settings',array('value'=>$val),array('name'=>$key));						
		}		
	}
	
	
	
}
?>
